﻿using System;
using System.Configuration;
using System.Web;
using System.Web.UI.WebControls;
using System.Collections.Generic;

namespace Shuba.Web.bulk_upload
{
    public partial class branch_rollout_version_edit : basePage
    {
        string msg = "";
        bool IsView, IsAdd, IsEdit, IsDelete, IsChecker;

        protected void Page_Load(object sender, EventArgs e)
        {
            string acl = GetUserAccess("MSTR_BRANCH_ROLLOUT");

            IsView = IsUserView(acl);
            IsAdd = IsUserAdd(acl);
            IsEdit = IsUserEdit(acl);
            IsDelete = IsUserDelete(acl);
            IsChecker = IsUserChecker(acl);

            if (!IsPostBack)
            {
                initVersion();

                if (IsChecker)
                {
                    string msg = this.GetGlobalResourceObject("Messages", "NoPrivilege").ToString();
                    AlertAndRedirect(msg, ResolveUrl(ConfigurationManager.AppSettings["default_url"]));
                }

                LabelTitle.Text = GetPageTitle("MSTR_BRANCH_ROLLOUT");

                string branch_id = string.Empty;
                string version = string.Empty;
                if (Request.QueryString["brc"] != null && Request.QueryString["vr"] != null)
                {
                    branch_id = Request.QueryString["brc"];
                    version = Request.QueryString["vr"];
                }
                if (branch_id != string.Empty && version != string.Empty)
                {
                    var bl = new Shuba.Business.branch_rollout_version();
                    Shuba.Model.branch_rollout_versionInfo bi;

                    bi = bl.Get(branch_id, version);
                    txtBranchId.Text = bi.BranchID;
                    ddlVersion.Text = bi.version;
                    txtImplDate.Text = bi.implementation_date.ToString("dd MMM yyyy");//dd MMM yyyy hh:mm:ss
                    if (bi.is_delete == "1") { rbDelete.Checked = true; }
                    if (bi.is_delete == "0") { rbNotDelete.Checked = true; }
                }

                //hidParam.Value = Request.QueryString["id"];

                if (IsUserAdd(acl))
                {
                    btnAdd.Enabled = true;
                    btnUpload.Enabled = true;
                }
                else
                {
                    btnAdd.Enabled = false;
                    btnUpload.Enabled = false;
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            var bsRoll = new Shuba.Business.branch_rollout_version();
            var info = new Shuba.Model.branch_rollout_versionInfo();
            info = bsRoll.Get(Request.QueryString["brc"], Request.QueryString["vr"]);

            string ActType = "";
            int ErrorCount = 0, SuccessCount = 0;
            DateTime implDate = Convert.ToDateTime(txtImplDate.Text);
            DateTime dateNow = DateTime.Today;

            if (implDate.Date < ProcessingDate().Date)
            {
                lblImplDate.Text = "Implementation Date harus lebih besar dari tanggal sekarang";
            }
            else 
            {
                info.BranchID = txtBranchId.Text;
                info.version = ddlVersion.SelectedValue;
                
                info.implementation_date = Convert.ToDateTime(txtImplDate.Text);
                if (rbDelete.Checked) { info.is_delete = "1"; }
                if (rbNotDelete.Checked) { info.is_delete = "0"; }

                info.update_by = HttpContext.Current.User.Identity.Name;
                info.update_date = ProcessingDate();
                info.ActType = "UPDATE";

                try
                {
                    if (bsRoll.Save(info) > 0)
                    {
                        SuccessCount++;
                        AddLog(ActType, ActType + " BRANCH ROLLOUT VERSION");
                    }
                    else
                    {
                        ErrorCount++;
                    }
                    //msg = this.GetGlobalResourceObject("Messages", "SaveAlert").ToString();
                }
                catch (Exception ex)
                {
                    //ErrBox.Visible = true;
                    //ErrMsg.Text = ex.Message;
                }
                if (SuccessCount > 0 || ErrorCount > 0)
                {
                    msg = "Save Notification\\n\\nSuccessfully Save : " + SuccessCount + " Record(s)\\n\\nFailed to Save : " + ErrorCount + " Record(s)";
                }
                AlertAndRedirect(msg, "branch_rollout_version_list.aspx");
            }
        }

        private void initVersion()
        {
            var bsRollout = new Shuba.Business.branch_rollout_version();
            ddlVersion.Items.Clear();
            ddlVersion.Items.Add(new ListItem("- SELECT -", ""));

            List<string> list = bsRollout.getVersion();
            foreach (string info in list)
            {
                ddlVersion.Items.Add(new ListItem(info, info));
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("branch_rollout_version_list.aspx");
        }
    }
}